import axios from 'axios';
import Vue from 'vue';
import {Toast} from 'mint-ui';

axios.defaults.timeout = 10000;
//跨域请求，允许保存cookie
axios.defaults.withCredentials = true;
let cfg, msg;
msg = '服务器君开小差了，请稍后再试';
//HTTPrequest拦截
axios.interceptors.request.use(config => {
	// if (store.getters.token) {
	// 	config.headers['X-Token'] = getToken() // 让每个请求携带token-- ['X-Token']为自定义key 请根据实际情况自行修改
	// }
	return config
}, error => {
	console.log('err' + error)// for debug
	return Promise.reject(error)
})
//HTTPresponse拦截
axios.interceptors.response.use(res => {
	// console.log(res);
	return res.data.data
}, error => {
	console.log(error.response.data.msg);
	  Toast(error.response.data.msg);
	let err = {};
	switch (error.response.status) {
		case 400:
		  err.message = '请求错误'
		  break

		case 401:
		  err.message = '未授权，请登录'
		  break

		case 403:
		  err.message = '拒绝访问'
		  break

		case 404:
		  err.message = `请求地址出错: ${err.response.config.url}`
		  break

		case 408:
		  err.message = '请求超时'
		  break

		case 500:
		  err.message = '服务器内部错误'
		  break

		case 501:
		  err.message = '服务未实现'
		  break

		case 502:
		  err.message = '网关错误'
		  break

		case 503:
		  err.message = '服务不可用'
		  break

		case 504:
		  err.message = '网关超时'
		  break

		case 505:
		  err.message = 'HTTP版本不受支持'
		  break

		default:
	  }
	return Promise.reject(new Error(msg));

})

export default axios
